/*
 * Copyright 2021 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "lvgl_support.h"
#include "lvgl.h"
#include "lv_conf.h"

#include "FreeRTOS.h"
#include "semphr.h"

#include "xSYS_ST7789.h"

#include "xAPP_Config.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/* Cache line size. */
/*******************************************************************************
 * Prototypes
 ******************************************************************************/

static void DEMO_WaitFlush(lv_disp_drv_t *disp_drv);
static void DEMO_FlushDisplay(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p);



/*******************************************************************************
 * Variables
 ******************************************************************************/
static uint8_t s_frameBuffer[2][LCD_VIRTUAL_BUF_SIZE * LCD_FB_BYTE_PER_PIXEL];

/*******************************************************************************
 * Code
 ******************************************************************************/

void lv_port_pre_init(void)
{

}

/*
********************************************************************************************************************
@ Brief  : LCD 初始化，注册相关

@ Param  : None

@ Return : None

@ Author : LYC

@  Date  : 2018 - 12 - 24
********************************************************************************************************************
*/
void lv_port_disp_init(void)
{
    static lv_disp_draw_buf_t disp_buf;

    lv_disp_draw_buf_init(&disp_buf, s_frameBuffer[0], NULL, LCD_VIRTUAL_BUF_SIZE);  //s_frameBuffer[1]

    /*-------------------------
     * Initialize your display
     * -----------------------*/
    xSYS_ST7789_Init();

    /*-----------------------------------
     * Register the display in LittlevGL
     *----------------------------------*/
    static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
    lv_disp_drv_init(&disp_drv);   /*Basic initialization*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = LCD_WIDTH;
    disp_drv.ver_res = LCD_HEIGHT;

    /*Used to copy the buffer's content to the display*/
    disp_drv.flush_cb = DEMO_FlushDisplay;

    /*Set a display buffer*/
    disp_drv.draw_buf = &disp_buf;

    /* Partial refresh */
    disp_drv.full_refresh = 1;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}


static void DEMO_FlushDisplay(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
    // (void)xSemaphoreTake(s_frameSema, 0);
    lv_coord_t x1 = area->x1;
    lv_coord_t y1 = area->y1;
    lv_coord_t x2 = area->x2;
    lv_coord_t y2 = area->y2;

    uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;

    LCD_Address_Set(x1,y1,x2,y2);

    xSYS_LCD_Fill((uint8_t*)color_p,size);

    lv_disp_flush_ready(disp_drv);
}

// static void DEMO_WaitFlush(lv_disp_drv_t *disp_drv)
// {
//     if (xSemaphoreTake(s_frameSema, portMAX_DELAY) != pdTRUE)
//     {
//         //PRINTF("Display flush failed\r\n");
//         // assert(0);
//     }
// }
